#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>

int main()
{
	float PI = 3.1415926;
	int width = 800;
	int height = 600;
	initgraph(width, height);
	setbkcolor(RGB(255, 255, 255));
	setlinestyle(PS_SOLID, 3);

	float lineLength = 160;
	float xEnd, yEnd;
	float rotateSpeed = PI / 360;
	int lineNum = 0;
	float Angles[1000];
	int score = 0;
	int i;
	
	BeginBatchDraw();
	while (1)
	{
		cleardevice();
		setlinecolor(RGB(0, 0, 0));
		line(0, height / 2, lineLength, height / 2);
		for (i = 0; i < lineNum; i++)
		{
			Angles[i] = Angles[i] + rotateSpeed;
			if (Angles[i] >= 2 * PI)
			{
				Angles[i] = Angles[i] - 2 * PI;
			}
			xEnd = lineLength * cos(-Angles[i]) + width / 2;
			yEnd = lineLength * sin(-Angles[i]) + height / 2;
			setlinecolor(RGB(0, 0, 255));
			if (i == lineNum - 1)
			{
				setlinecolor(RGB(255, 0, 0));
			}
			line(width / 2, height / 2, xEnd, yEnd);
		}

		if (_kbhit() && rotateSpeed != 0)
		{
			char input = _getch();
			if (input == ' ')
			{
				lineNum++;
				Angles[lineNum - 1] = PI;
				xEnd = lineLength * cos(-Angles[lineNum - 1]) + width / 2;
				yEnd = lineLength * sin(-Angles[lineNum - 1]) + height / 2;
				line(width / 2, height / 2, xEnd, yEnd);
				for (i = 0; i < lineNum - 1; i++)
				{
					if (fabs(Angles[lineNum - 1] - Angles[i]) < PI / 60)
					{
						rotateSpeed = 0;
						break;
					}
				}
				score = score + 1;
			}
		}
		setfillcolor(HSVtoRGB(0, lineNum / 60.0, 0.8));
		setlinecolor(HSVtoRGB(0, 0.9, 0.8));
		fillcircle(width / 2, height / 2, 60);
		TCHAR s[20];
		sprintf_s(s, _T("%d"), score);
		settextstyle(50, 0, _T("Times"));
		settextcolor(RGB(50, 50, 50));
		outtextxy(65, 200, s);

		FlushBatchDraw();
		Sleep(10);
	}
	closegraph();

	return 0;
}